package com.example.wzh.controller;

import com.example.wzh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public String login(Model model){
        return "login";
    }
    @PostMapping("doLogin")
    public String doLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                         Model model){
        if (userService.isValid(username, password)){
            model.addAttribute("hello", "欢迎进入Thymeleaf页面");
            model.addAttribute("username", username);
            return "success";
        } else {
            model.addAttribute("error", "用户登入失败");
            return "error";
        }
    }

}
